<?php
namespace interface\v1;
/*
*固定资产接口类
*/


class assetAppClass extends index
{
    private const page = 'asset';
    private const table = 'asset';
    private const title = '来访登记';
    /*详情*/
    public function detail()
    {
        $code = $this->post('code');
        $info = query(self::table,"orderNo = '$code'");
        if (empty($info['id'])){
            $this->resp['warn'] = '未找到此资产';
        }else{
            $this->code = 1;
            $info['type'] = self::getCatalogName($info['acid']);
            $info['manager'] = self::findStaff($info['manager']);
            $salvage    = ($info['purchaseMoney'] && $info['discountYear'] && $info['purchaseDate']) ? self::calcSalvage(
                (float)$info['purchaseMoney'],
                (int)$info['discountYear'],
                (string)$info['purchaseDate']
            ) : '条件不完整';
            $info['salvage'] = $salvage;

            $record = [];
            $all = find(array('table'=>'asset_revert','where'=>"atid = '$info[id]' order by time desc"));
            foreach ($all['array'] as $array){
                $record[] = [
                    'id'=>$array['id'],
                    'type'=>$array['type'],
                    'userDepartment'=>$array['userDepartment']?:' ',
                    'user'=>$array['user']?:' ',
                    'storage'=>$array['storage']?:' ',
                    'time'=>$array['time'],
                ];
            }

            $data['result'] = [
                'info' => $info,
                'record' => $record
            ];
            $this->resp['data'] = $data;
        }
    }
    /**
     * 计算残值
     *
     * @param float $purchaseMoney  购置金额
     * @param int $discountYear 折价期限
     * @param string $purchaseDate 购置日期
     * @return float
     */
    public static function calcSalvage(float $purchaseMoney,  int $discountYear, string $purchaseDate):float
    {
        $days = (strtotime(date('Y-m-d')) - strtotime($purchaseDate)) / (60*60*24);
        $month = ceil($days / 30);
        // 当前残值=购置金额-购置金额/折价期限*（MONTH（当前日期-购置日期）+1）
        $money = $purchaseMoney - $purchaseMoney / $discountYear * $month;
        return round($money, 2);
    }
    /**
     * 获取分类名
     *
     * @param string $id
     */
    public static function getCatalogName(string $id): string
    {
        $temp = '';
        if ($id) {
            $info = query('asset_catalog', " id = '$id' ");
            if ($info['pid'] == '0') {
                $temp .= $info['name'];
            } else {
                $temp .= self::getCatalogName($info['pid']) . ' > ' . $info['name'];
            }
        }
        return $temp;
    }
}